package com.tonann.app.service.impl;

import com.tonann.app.VO.FaultVO;
import com.tonann.app.domain.AppFault;
import com.tonann.app.domain.AppFaultHandle;
import com.tonann.app.mapper.AppFaultMapper;
import com.tonann.app.service.IAppFaultHandleService;
import com.tonann.app.service.IAppFaultService;
import com.tonann.common.core.redis.RedisCache;
import com.tonann.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 故障记录Service业务层处理
 *
 * @author tonann
 * @date 2022-10-15
 */
@Service
public class AppFaultServiceImpl implements IAppFaultService
{
    @Resource
    private AppFaultMapper appFaultMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private IAppFaultHandleService faultHandleService;

    /**
     * 查询故障记录
     *
     * @param faultId 故障记录主键
     * @return 故障记录
     */
    @Override
    public AppFault selectAppFaultByFaultId(Long faultId)
    {
        return appFaultMapper.selectAppFaultByFaultId(faultId);
    }

    /**
     * 查询故障记录列表
     *
     * @param appFault 故障记录
     * @return 故障记录
     */
    @Override
    public List<AppFault> selectAppFaultList(AppFault appFault)
    {
        return appFaultMapper.selectAppFaultList(appFault);
    }

    /**
     * 新增故障记录
     *
     * @param appFault 故障记录
     * @return 结果
     */
    @Override
    public int insertAppFault(AppFault appFault)
    {
        AppFault fault = new AppFault();
        fault.setFaultRuleId(appFault.getFaultRuleId());
        fault.setDeviceId(appFault.getDeviceId());
        fault.setSensorId(appFault.getSensorId());
        List<AppFault> faultList = this.selectAppFaultList(fault);
        Set<Long> set = new HashSet<>();
        for(AppFault f : faultList){
            set.add(f.getFaultId());
        }
        if (set.size() > 0){
            List<AppFaultHandle> faultHandleList =
                    faultHandleService.selectAppFaultHandleListByFaultIds(
                            set.toArray(set.toArray(new Long[set.size()]))
                    );
            if (faultHandleList.size() > 0){
                return  1;
            }
        }
        appFault.setCreateTime(DateUtils.getNowDate());
        int flag = appFaultMapper.insertAppFault(appFault);
        if (appFault.getFaultId() != null){
            AppFaultHandle faultHandle = new AppFaultHandle();
            faultHandle.setFaultId(appFault.getFaultId());
            faultHandle.setIsChecked("0");
            faultHandle.setCreateTime(appFault.getCreateTime());
            faultHandle.setCreateBy(appFault.getCreateBy());
            faultHandleService.insertAppFaultHandle(faultHandle);
        }
        return  flag;
    }

    /**
     * 修改故障记录
     *
     * @param appFault 故障记录
     * @return 结果
     */
    @Override
    public int updateAppFault(AppFault appFault)
    {
        appFault.setUpdateTime(DateUtils.getNowDate());
        return appFaultMapper.updateAppFault(appFault);
    }

    /**
     * 批量删除故障记录
     *
     * @param faultIds 需要删除的故障记录主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultByFaultIds(Long[] faultIds)
    {
        return appFaultMapper.deleteAppFaultByFaultIds(faultIds);
    }

    /**
     * 删除故障记录信息
     *
     * @param faultId 故障记录主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultByFaultId(Long faultId) {
        return appFaultMapper.deleteAppFaultByFaultId(faultId);
    }

    @Override
    public List<FaultVO> getDeviceFaultName(Set<Long> set) {
        return appFaultMapper.selectDeviceFaultNameList(set);
    }

    @Override
    public List<AppFault> selectAppFaultByDeviceIds(Set<Long> set, Long userId, String isRead, String orderLevel, String orderTime) {
        return appFaultMapper.selectAppFaultByDeviceIds(set, userId, isRead, orderLevel, orderTime);
    }

    @Override
    public List<AppFault> selectUnCheckedAppFaultListByDeviceId(Long deviceId) {
        List<AppFault> appFaultList = null;
//        if ( !redisCache.hasKey("app_fault_list::device_id::" + deviceId)){
            appFaultList = appFaultMapper.selectUnCheckedAppFaultListByDeviceId(deviceId);
//            if (appFaultList != null && appFaultList.size() > 0) {
//                redisCache.setCacheList("app_fault_list::device_id::" + deviceId, appFaultList);
//            }else {
//                appFaultList = new ArrayList<>();
//            }
//        }else{
//           appFaultList =  redisCache.getCacheList("app_fault_list::device_id::" + deviceId);
//        }
        return  appFaultList;
    }

    @Override
    public List<AppFault> selectUnCheckedAppFaultListByDeviceIds(Set<Long> set) {
        return appFaultMapper.selectUnCheckedAppFaultListByDeviceIds(set);
    }
}
